package com.google.bitcoin.core;

import com.google.common.collect.ImmutableList;

import java.util.Collection;

/**
 * @author apetersson
 */
public class SingleKey implements TransactionSigner {

    final ECKey myKeyPart;

    public SingleKey(ECKey myKeyPart) {
        this.myKeyPart = myKeyPart;
    }


    //todo check why this is called quite often per multisig tx..
    public Collection<PubKey> canSign(Collection<PubKey> requestedKeys) {
        PubKey pubKey = myKeyPart.getPublicKey();
        for (PubKey requestedKey : requestedKeys) {
            if (requestedKey.equals(pubKey)) {
                return ImmutableList.of(pubKey);
            }
        }
        return ImmutableList.of();//empty list;
    }

    public byte[] sign(Transaction transaction, PubKey pubKey, int inputIndex, Sha256Hash hashBytes) {
        if (!pubKey.equals(myKeyPart.getPublicKey())) {
            throw new IllegalArgumentException("unable to sign this key...");
        }
        return myKeyPart.sign(hashBytes.getBytes());
    }
}
